package com.boot.learning.captcha;


import java.util.Arrays;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * 数字验证码
 * 
 * 默认4位数字 
 * @version 1.0
 * @author fuhw
 */
public class NumberCaptchaGenerator implements CaptchaGenerator {

	private final static int DEFAULT_LENGTH = 4;

	@Setter
	@Getter
	private int length = DEFAULT_LENGTH;

	@Override
	public String gener(){
		Random random = new Random();
		String[] arr = new String[length];
		Arrays.fill( arr, "9" );
		int presentedRandom = random.nextInt(Integer.parseInt( StringUtils.join( arr ) ));
		String captcha = String.format( "%0" + length + "d", presentedRandom );
		return captcha;
	}
}
